package com.zhishu.portal.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhishu.model.Result;
import com.zhishu.model.User;
import com.zhishu.portal.common.result.USession;
import com.zhishu.portal.service.UserService;
import com.zhishu.portal.util.Const;

@Controller
@RequestMapping("/action/user")
public class UserAction {
	private static Logger logger = Logger.getLogger(UserAction.class);
	@Resource
	private UserService userService;

	@ResponseBody
	@RequestMapping(value = "/u/login", method = RequestMethod.POST)
	public Result login(HttpServletRequest request, HttpServletResponse response, String username, String password) {
		Result ret = new Result();
		try {
			ret = userService.login(username, password,null);
			if (ret.getCode().equals(200)) {
				USession us = new USession();
				User u = (User) ret.getData();
				us.setId(u.getId());
				us.setUser_name(u.getUser_name());
				request.getSession().setAttribute(Const.SESSION_KEY, us);
			}
		} catch (Exception e) {
			logger.error("", e);
		}

		return ret;

	}

	@ResponseBody
	@RequestMapping(value = "/u/islogin", method = RequestMethod.POST)
	public Result isLogin(HttpServletRequest request, HttpServletResponse response, String username, String password) {
		Result ret = new Result();
		try {
			Object session = request.getSession().getAttribute(Const.SESSION_KEY);
			if (session != null) {
				USession us = (USession) session;
				ret.setResult(Const.SUC, us);
			} else {
				ret.setResult(Const.FAIL, "没有登录");
			}
		} catch (Exception e) {
			logger.error("", e);
			ret.setResult(Const.FAIL, "没有登录");
		}

		return ret;

	}

	@ResponseBody
	@RequestMapping(value = "/u/regUser", method = RequestMethod.POST)
	public Result regUser(HttpServletRequest request, HttpServletResponse response, User user) {
		Result ret = new Result();
		try {
			ret = userService.regUser(user);
			if (ret.getCode().equals(200)) {
				USession us = new USession();
				User u = (User) ret.getData();
				us.setId(u.getId());
				us.setUser_name(u.getUser_name());
				request.getSession().setAttribute(Const.SESSION_KEY, us);
			}
		} catch (Exception e) {
			logger.error("", e);
		}

		return ret;

	}
	
	@ResponseBody
	@RequestMapping(value = "u/sendCode", method = RequestMethod.POST)
	public Result sendCode(HttpServletRequest request, HttpServletResponse response,String username,String type) {
		Result ret = new Result();
		try {
			ret = userService.sendCode(username,type);
		} catch (Exception e) {
			logger.error("", e);
		}
		return ret;

	}

}
